﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using DAL;

namespace PeisongWeb.admin
{
    public partial class recipeCategoryEdit : adminPage
    {
        protected int id
        {
            get
            {
                int tmp;
                try { tmp = Convert.ToInt32(ViewState["id"]); }
                catch { tmp = 0; }
                return tmp;
            }
            set
            { ViewState["id"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (hasPermission("BF", 1))
                {
                    if (Request.QueryString["category"] != null)
                    {
                        if (Library.Number.IsIntPositive(Request.QueryString["category"]))
                        {
                            id = Convert.ToInt32(Request.QueryString["category"]);
                        }
                    }
                    drpBind();
                    if (id != 0)
                    {

                        getDetails();
                    }
                }
                else
                {
                    Library.Script.ClientMsgUrl("没有权限！", "index.aspx");
                    return;
                }
            }
        }

        private void getDetails()
        {
            RecipeCategoryEntity category = logic.recipeCategory.getById(id);
            txt_Name.Text = category.Name.Trim();

            txt_Treelevel.Text = category.TreeLevel.ToString();
            txt_Sort.Text = category.Sort.ToString();
            drp_ParentCategory.SelectedIndex = drp_ParentCategory.Items.IndexOf(drp_ParentCategory.Items.FindByValue(category.ParentId.ToString()));
            chx_isShow.Checked = category.IsShow;
        }
        private void drpBind()
        {
            drp_ParentCategory.DataSource = logic.recipeCategory.select("treelevel=0", "rcid", 1000, 1).Tables[1];
            drp_ParentCategory.DataBind();
            drp_ParentCategory.Items.Insert(0, new ListItem("--无--", "0"));
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            RecipeCategoryEntity cat = new RecipeCategoryEntity();
            cat.Name = txt_Name.Text.Trim();
            cat.Sort = txt_Sort.Text != "" ? Convert.ToInt32(txt_Sort.Text) : 1;
            short treeLevel = 0;
            short.TryParse(txt_Treelevel.Text,out treeLevel);
            cat.TreeLevel = treeLevel;
            cat.ParentId = Convert.ToInt32(drp_ParentCategory.SelectedItem.Value);
            cat.IsShow = chx_isShow.Checked;
            if (id != 0)
            {
                cat.Rcid = id;
                logic.recipeCategory.update(cat);
            }
            else
            {
                logic.recipeCategory.insert(cat);
            }

            Library.Script.ClientMsgUrl("保存成功！", "recipeCategoryList.aspx");

        }
    }
}
